import dir from '../../assets/images/dir.jpg';
import up from '../../assets/images/up.jpg';
import up_active from '../../assets/images/up_active.jpg';
import info from '../../assets/images/info.jpg';
import info_active from '../../assets/images/info_active.jpg';
import user from '../../assets/images/user.jpg';
import user_active from '../../assets/images/user_active.jpg';
import down from '../../assets/images/down.jpg';
import down_active from '../../assets/images/down_active.jpg';
import { getTaskList,taskClear } from '../../api/index.js'
import {dateFormatDate,dateFormat} from '../../utils/index.js'
Page({
  data() {
    return {
      up,
      up_active,
      info,
      info_active,
      user,
      user_active,
      down,
      down_active,
      panType: '3',
      customList: [],
      tabBarActiveTextStyle: {
        'font-size': '16px',
        'font-weight': '700',
        'color':'#333333'
      },
      tabBarTextStyle:{
        'font-size': '14px',
        'color':'#808080'
      },
      activeTab:null,
      dateList:[],
      flag:false,
      
    }
  },
  methods: {
    handleArray(arr){
      if(!arr) return
      let obj = {}
      const list = []
      arr.forEach(val=>{
        let time = dateFormatDate(val.taskTime)
        if(obj[time]){
          obj[time].push(val)
        }else{
          obj[time]=[val]
        }
      })
      for (const key in obj) {
        if (Object.hasOwnProperty.call(obj, key)) {
          
          list.push({
            date: key===dateFormatDate(dateFormat(new Date())) ? '今天' : key.slice(5),
            dateList:obj[key]
          })
        }
      }

      this.dateList = [...list]
    },
    onTabClick(item){
      this.currentPanType = Number(item.index)+2
      
      this.getTaskList()

      clearInterval(this.flag)
      this.flag = setInterval(()=>{
        this.getTaskList()
      },1000*10)
    },
 
    getTaskList(){

      getTaskList({
        "panType": this.currentPanType,
      }).then(res => {
        if (res.data.code === 500&&res.data.msg==='用户登录已超时，请重新登录') {
          ok.clearH5AppStorage().then(res=>{
            this.$router.replace('/pages/login/index')
          })
          .catch(err=> {
            console.log("接口调用异常",err);
          });
          
          return
        }
        this.customList = res.data.data
        let flag = true
        this.customList.forEach((val)=>{
          // 有一个不是100%就需要轮询
          if(parseInt(val.taskRate) !== 100){
            flag = false
          }
        })
        // 都是100%  那就关闭轮询
        if(flag){
          clearInterval(this.flag)
        }
        this.handleArray(this.customList)


        
      })
    },
    clearTask(){
      console.log("val===>",6)
      this.$Modal.confirm({
        title: '一键清空',
        content: '当前操作将清空传输列表，仅保留“下载中”的任务。',
        submitText: '确认',
        cancelText: '取消',
        onCancel: () => {
          
        },
        onSubmit: () => {
          taskClear({
            "panType": this.panType,
          }).then(res => {
              if(res.data.code===0){
                this.getTaskList()
              }
          })
        }
      })
     
    }
  },
  mounted() {
    ok.getH5AppStorage({
      key: 'panType'
    }).then(res => {
      this.panType = res.data.data
      this.activeTab = Number(this.panType)-2
      // console.log('现在的==>',this.activeTab);
      this.currentPanType = this.panType
      this.getTaskList()


      this.flag = setInterval(()=>{
        this.getTaskList()
      },1000*10)
    }).catch(err => {
      console.log("接口调用异常", err);
    });
  },
  beforeDestroy(){
    clearInterval(this.flag)
  }
});
